package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import pos.domain.Estacion;

public class JDBCEstacionDAO implements IEstacionDAO {
	
	Connection conn = ConnectionManager.getInstance().getConn();

	@Override
	public Estacion selectByNumero(int idEstacion) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		Estacion estacion = null;
		String sql = "SELECT * FROM estacionsevici WHERE (numero = ?) ";
		
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, idEstacion);
			result = stmt.executeQuery();

			if(result.next()){
				estacion = new Estacion();
				estacion.setNumero(result.getInt("numero"));
				estacion.setDescripcion(result.getString("descripcion"));
				estacion.setIdEstacion(result.getInt("idEstacionSevici"));
			}
			
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return estacion;
	}
}
